//
//  WmcEditCell.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/10.
//

import UIKit
import YPBedrock

class WmcEditWatermarkAttributeCell: WmcEditWatermarkBaseCell {
    /// 时间
    lazy var timeLabel: WmcDateLabel = {
        let label = WmcDateLabel.YP.Create(font: 14.wmc_font,
                                           color: UIColor.wmc_grayH11_9D9DB3,
                                           alignment: .left)
        label.leftEdge = 0
        label.numberOfLines = 0
        label.isHidden = true
        return label
    }()

    /// 地点
    lazy var locationLabel: WmcLocationLabel = {
        let label = WmcLocationLabel.YP.Create(font: 14.wmc_font,
                                               color: UIColor.wmc_grayH11_9D9DB3,
                                               alignment: .left)
        label.leftEdge = 0
        label.numberOfLines = 0
        label.isHidden = true
        return label
    }()

    /// 天气
    lazy var weatherLabel: WmcWeatherLabel = {
        let label = WmcWeatherLabel.YP.Create(font: 14.wmc_font,
                                              color: UIColor.wmc_grayH11_9D9DB3,
                                              alignment: .left)
        label.leftEdge = 0
        label.numberOfLines = 0
        label.isHidden = true
        return label
    }()

    /// 海拔
    lazy var altitudeLabel: WmcAltitudeLabel = {
        let label = WmcAltitudeLabel.YP.Create(font: 14.wmc_font,
                                               color: UIColor.wmc_grayH11_9D9DB3,
                                               alignment: .left)
        label.leftEdge = 0
        label.numberOfLines = 0
        label.isHidden = true
        return label
    }()

    /// 方位角
    lazy var azimuthAngleLabel: WmcAzimuthAngleLabel = {
        let label = WmcAzimuthAngleLabel.YP.Create(font: 14.wmc_font,
                                                   color: UIColor.wmc_grayH11_9D9DB3,
                                                   alignment: .left)
        label.leftEdge = 0
        label.numberOfLines = 0
        label.isHidden = true
        return label
    }()

    /// 分贝
    lazy var decibeLabel: WmcDecibeLabel = {
        let label = WmcDecibeLabel.YP.Create(font: 14.wmc_font,
                                             color: UIColor.wmc_grayH11_9D9DB3,
                                             alignment: .left)
        label.leftEdge = 0
        label.numberOfLines = 0
        label.isHidden = true
        return label
    }()

    /// 速度
    lazy var speedLabel: WmcSpeedLabel = {
        let label = WmcSpeedLabel.YP.Create(font: 14.wmc_font,
                                            color: UIColor.wmc_grayH11_9D9DB3,
                                            alignment: .left)
        label.leftEdge = 0
        label.numberOfLines = 0
        label.isHidden = true
        return label
    }()

    override func makeUI() {
        addSubview(switchBtn)
        switchBtn.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(Wmc_standardEdge.left)
        }

        addSubview(arrowImageView)
        arrowImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-Wmc_standardEdge.right)
            make.size.equalTo(17)
        }

        titleLabel.font = 14.yp.uiFont(weight: .medium)
        titleLabel.textColor = UIColor.wmc_grayH2_323233
        titleLabel.textAlignment = .left
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(Wmc_standardEdge.top / 2)
            make.height.equalTo(18)
            make.left.equalTo(switchBtn.snp.right).offset(8)
            make.right.equalTo(arrowImageView.snp.left).offset(-8)
        }

        addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.left.right.height.equalTo(titleLabel)
            make.bottom.equalTo(-Wmc_standardEdge.bottom)
        }

        addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.edges.equalTo(contentLabel)
        }

        addSubview(locationLabel)
        locationLabel.snp.makeConstraints { make in
            make.edges.equalTo(contentLabel)
        }

        addSubview(weatherLabel)
        weatherLabel.snp.makeConstraints { make in
            make.edges.equalTo(contentLabel)
        }

        addSubview(altitudeLabel)
        altitudeLabel.snp.makeConstraints { make in
            make.edges.equalTo(contentLabel)
        }

        addSubview(azimuthAngleLabel)
        azimuthAngleLabel.snp.makeConstraints { make in
            make.edges.equalTo(contentLabel)
        }

        addSubview(decibeLabel)
        decibeLabel.snp.makeConstraints { make in
            make.edges.equalTo(contentLabel)
        }

        addSubview(speedLabel)
        speedLabel.snp.makeConstraints { make in
            make.edges.equalTo(contentLabel)
        }

        addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
            make.left.equalTo(titleLabel.snp_leftMargin)
            make.right.equalTo(arrowImageView.snp_rightMargin)
        }
    }

    override func configModel(_ model: WmcWatermarkOptionModel,
                              styleMode: WmcBaseWatermarkTemplateView.StyleMode,
                              watermarkModel: WmcWatermarkDetailModel)
    {
        let model = model
        titleLabel.text = model.title
        contentLabel.text = model.contentText

        contentLabel.isHidden = true
        timeLabel.isHidden = true
        locationLabel.isHidden = true
        weatherLabel.isHidden = true
        altitudeLabel.isHidden = true
        azimuthAngleLabel.isHidden = true
        decibeLabel.isHidden = true
        speedLabel.isHidden = true
        if !model.isOpen {
            contentLabel.isHidden = false
            contentLabel.text = "已隐藏"
        } else {
            switch model.fieldType {
            case .time:
                timeLabel.styleMode = styleMode
                timeLabel.isHidden = false
                if let staticDate = watermarkModel.staticData.staticDate, styleMode.isStatic { // 需要静态
                    timeLabel.updateDate(staticDate)
                }
            case .addr:
                locationLabel.styleMode = styleMode
                locationLabel.format = .location
                locationLabel.isHidden = false
                if let staticAddress = watermarkModel.staticData.drawStaticAddress, styleMode.isStatic { // 需要静态
                    locationLabel.updateLocation(staticAddress)
                }
            case .weather:
                weatherLabel.styleMode = styleMode
                weatherLabel.isHidden = false
                if let staticWeater = watermarkModel.staticData.staticWeater, styleMode.isStatic { // 需要静态
                    weatherLabel.updateWeather(staticWeater)
                }
            case .lat:
                locationLabel.styleMode = styleMode
                locationLabel.format = .longitudLatitude
                locationLabel.isHidden = false
                if let staticAddress = watermarkModel.staticData.drawStaticAddress, styleMode.isStatic { // 需要静态
                    locationLabel.updateLocation(staticAddress)
                }
            case .altitude:
                altitudeLabel.styleMode = styleMode
                altitudeLabel.format = .meters
                altitudeLabel.isHidden = false
                if let staticLocation = watermarkModel.staticData.staticLocation, styleMode.isStatic { // 需要静态
                    altitudeLabel.updateLocation(staticLocation)
                }
            case .azimuth:
                azimuthAngleLabel.styleMode = styleMode
                azimuthAngleLabel.format = .`default`
                azimuthAngleLabel.isHidden = false
                if let staticAzimuth = watermarkModel.staticData.staticAzimuth, styleMode.isStatic { // 需要静态
                    azimuthAngleLabel.updateAzimuthAngle(staticAzimuth)
                }
            case .decibe:
                decibeLabel.styleMode = styleMode
                decibeLabel.format = .describeDB
                decibeLabel.isHidden = false
            case .speed:
                speedLabel.styleMode = styleMode
                speedLabel.format = .km_h
                speedLabel.isHidden = false
                if let staticLocation = watermarkModel.staticData.staticLocation, styleMode.isStatic {
                    speedLabel.updateLocation(staticLocation)
                }

            default:
                contentLabel.isHidden = false
            }
        }

        switchBtn.isEnabled = model.isAction
        arrowImageView.isHidden = !(model.enableEditTitle || model.enableEditContent)
        switchBtn.isOn = model.isOpen

        switchBtn.yp.addEvent { [weak self] switc in
            guard let weakSelf = self else { return }
            if model.fieldType == .decibe {
                // 分贝要判断权限
                let microphoneStauts = WmcAuthorizedManager.microphone()
                switch microphoneStauts {
                // 授权
                case .authorized,
                     .limited,
                     .restricted:
                    WmcDecibeManager.shared.start()
                    model.isOpen = switc.isOn
                    weakSelf.switchAnctionBlock?(weakSelf.indexPath, model)
                // 未授权
                case .notDetermined:
                    switc.isOn = false
                    model.isOpen = false
                    WmcAuthorizedManager.microphoneRequest()
                // 其他
                default:
                    switc.isOn = false
                    model.isOpen = false
                    WmcAuthorizedManager.showPrivacyAlert(type: .microphone)
                }
            } else {
                model.isOpen = switc.isOn
                weakSelf.switchAnctionBlock?(weakSelf.indexPath, model)
            }
        }
    }
}
